package cn.sxgan.gen.services.base.impl;

import cn.sxgan.gen.mappers.MySqlBaseQueryMapper;
import cn.sxgan.gen.model.ColumnInfo;
import cn.sxgan.gen.model.DbTable;
import cn.sxgan.gen.services.base.DataSourceQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: Mysql基础查询服务
 * @Author: sxgan
 * @Date: 2025-07-16 17:49
 * @Version: 1.0
 **/
@Slf4j
@Service
public class MysqlQueryImpl implements DataSourceQuery {
    
    @Autowired
    private DataSource dataSource;
    @Autowired
    private MySqlBaseQueryMapper mySqlBaseQueryMapper;
    
    @Override
    public List<DbTable> selectDbTables(String tableName) {
        return mySqlBaseQueryMapper.selectTableAndFieds(tableName);
    }
    
    @Override
    public List<ColumnInfo> getTableColumns(String tableName) throws SQLException {
        List<ColumnInfo> columns = new ArrayList<>();
        try  (Connection conn = dataSource.getConnection()) {
            String dbName = conn.getCatalog();
            return mySqlBaseQueryMapper.selectTableColumns(tableName,dbName);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常", e);
        }
        return columns;
    }
}
